//
//  ICRedPacketPasswordViewController.m
//  ICome
//
//  Created by 席鹏飞 on 2016/11/8.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICRedPacketPasswordViewController.h"
#import "ICActPwdView.h"
#import "ZYKeyboardUtil.h"
#import "ICRedPacketNetworkManager.h"
#import "ICHeaderFile.h"

@interface ICRedPacketPasswordViewController ()
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UILabel *icomLabel;
@property (nonatomic, strong) UILabel *setPayLabel;
@property (nonatomic, strong) UILabel *confirmPayLabel;
@property (nonatomic, strong) UILabel *promptLabel;
@property (nonatomic, strong) UIButton *finishButton;
@property (nonatomic, strong) ICActPwdView *actPwdView;
@property (nonatomic, strong) ZYKeyboardUtil *keyboardUtil;
@end

@implementation ICRedPacketPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setUI];
    [self setNav];
    [self setKeyboardUtil];
    [self addObserver];
    [self getData];
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self setNavBackgroundColor:NE_REDPACKET_BACKGROUND_COLOR];
    [self setNavShadowColor:NE_REDPACKET_BACKGROUND_COLOR];
}


- (void)addObserver {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextChanged:) name:UITextFieldTextDidChangeNotification object:self.actPwdView.oldPasswardTextField];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextChanged:) name:UITextFieldTextDidChangeNotification object:self.actPwdView.newPasswardTextField];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextChanged:) name:UITextFieldTextDidChangeNotification object:self.actPwdView.reNewPasswardTextField];
}


- (void)textFieldTextChanged:(NSNotification *)notification {
    UITextField *textField = notification.object;
    NSString *iComPwd = self.actPwdView.oldPasswardTextField.text;
    NSString *setPayPwd = self.actPwdView.newPasswardTextField.text;
    NSString *confirmPayPwd = self.actPwdView.reNewPasswardTextField.text;

    if ([textField isEqual:self.actPwdView.oldPasswardTextField]) {
        if (textField.text.length > 20) {
            self.actPwdView.oldPasswardTextField.text = [textField.text substringToIndex:20];
        }
    }else if ([textField isEqual:self.actPwdView.newPasswardTextField]) {
        if (textField.text.length >= 6) {
            self.actPwdView.newPasswardTextField.text = [textField.text substringToIndex:6];
            [self pwd:setPayPwd comparePwd:confirmPayPwd iComPwd:iComPwd];
        }
    }else if ([textField isEqual:self.actPwdView.reNewPasswardTextField]) {
        if (textField.text.length >= 6) {
            self.actPwdView.reNewPasswardTextField.text = [textField.text substringToIndex:6];
            [self pwd:confirmPayPwd comparePwd:setPayPwd iComPwd:iComPwd];
        }
    }
    
    iComPwd = self.actPwdView.oldPasswardTextField.text;
    setPayPwd = self.actPwdView.newPasswardTextField.text;
    confirmPayPwd = self.actPwdView.reNewPasswardTextField.text;
    self.finishButton.enabled = (iComPwd.length > 0 && setPayPwd.length == 6 && confirmPayPwd.length == 6 && ([setPayPwd isEqualToString:confirmPayPwd]));
    if (self.finishButton.isEnabled) {
        [self.finishButton setTitleColor:[UIColor colorWithWhite:1.0 alpha:1.0] forState:UIControlStateNormal];
    }else {
        [self.finishButton setTitleColor:[UIColor colorWithWhite:1.0 alpha:0.4] forState:UIControlStateNormal];
    }
}


- (void)pwd:(NSString *)pwd comparePwd:(NSString *)comparePwd iComPwd:(NSString *)iComPwd {
    if (pwd.length == 6 && comparePwd.length > 0) {
        if ([pwd isEqualToString:comparePwd]) {
            self.promptLabel.text = @"";
        }else {
            self.promptLabel.text = @"两次密码不一致";
        }
    }
}

#pragma mark - private methods

// 设置页面布局
- (void)setUI {
    self.actPwdView.finishButton.hidden = YES;
    self.actPwdView.newPasswardTextField.keyboardType = UIKeyboardTypeNumberPad;
    self.actPwdView.reNewPasswardTextField.keyboardType = UIKeyboardTypeNumberPad;
    self.actPwdView.oldPasswardTextField.placeholder = @"请输入iCom密码";
    self.actPwdView.newPasswardTextField.placeholder = @"请输入6位数字密码";
    self.actPwdView.reNewPasswardTextField.placeholder = @"请确认6位数字密码";
    self.actPwdView.oldPasswardTextField.layer.cornerRadius = 5;
    self.actPwdView.newPasswardTextField.layer.cornerRadius = 5;
    self.actPwdView.reNewPasswardTextField.layer.cornerRadius = 5;
    
    [self.icomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.scrollView.mas_top).offset(36);
        make.left.equalTo(self.view.mas_left).offset(15);
        make.right.equalTo(self.view.mas_right).offset(-15);
        make.height.mas_equalTo(20);
    }];
    [self.actPwdView.oldPasswardTextField mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.icomLabel.mas_bottom).offset(10);
        make.left.equalTo(self.icomLabel.mas_left);
        make.right.equalTo(self.icomLabel.mas_right);
        make.height.mas_equalTo(45);
    }];
    [self.setPayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.actPwdView.oldPasswardTextField.mas_bottom).offset(15);
        make.left.equalTo(self.icomLabel.mas_left);
        make.right.equalTo(self.icomLabel.mas_right);
        make.height.equalTo(self.icomLabel.mas_height);
    }];
    [self.actPwdView.newPasswardTextField mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.setPayLabel.mas_bottom).offset(10);
        make.left.equalTo(self.icomLabel.mas_left);
        make.right.equalTo(self.icomLabel.mas_right);
        make.height.mas_equalTo(45);
    }];
    [self.confirmPayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.actPwdView.newPasswardTextField.mas_bottom).offset(15);
        make.left.equalTo(self.icomLabel.mas_left);
        make.right.equalTo(self.icomLabel.mas_right);
        make.height.equalTo(self.icomLabel.mas_height);
    }];
    [self.actPwdView.reNewPasswardTextField mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.confirmPayLabel.mas_bottom).offset(10);
        make.left.equalTo(self.icomLabel.mas_left);
        make.right.equalTo(self.icomLabel.mas_right);
        make.height.mas_equalTo(45);
    }];
    [self.promptLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.actPwdView.reNewPasswardTextField.mas_bottom).offset(10);
        make.left.equalTo(self.icomLabel.mas_left);
        make.right.equalTo(self.icomLabel.mas_right);
        make.height.equalTo(self.icomLabel.mas_height);
    }];
    [self.finishButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.actPwdView.reNewPasswardTextField.mas_bottom).offset(72);
        make.left.equalTo(self.icomLabel.mas_left);
        make.right.equalTo(self.icomLabel.mas_right);
        make.height.mas_equalTo(45);
    }];
}
// 设置navigation
- (void)setNav {
    self.title = @"设置密码";
    
    [self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamedFromiComeSDKBundle:@"App_back_white"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction:)]];
}
// 设置键盘
- (void)setKeyboardUtil {
    WEAKSELF;
    self.keyboardUtil = [[ZYKeyboardUtil alloc] init];
    [self.keyboardUtil setAnimateWhenKeyboardAppearAutomaticAnimBlock:^(ZYKeyboardUtil *keyboardUtil) {
        [keyboardUtil adaptiveViewHandleWithController:weakSelf adaptiveView:weakSelf.actPwdView.oldPasswardTextField, weakSelf.actPwdView.newPasswardTextField, weakSelf.actPwdView.reNewPasswardTextField, nil];
    }];
}
// 网络请求
- (void)getData {
    
}

#pragma mark - event response

- (void)leftButtonAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)finishButtonAction:(UIButton *)sender {
    NSString *iComPwd = self.actPwdView.oldPasswardTextField.text;
    NSString *setPayPwd = self.actPwdView.newPasswardTextField.text;

    WEAKSELF
    [MBProgressHUD showProcessingActivityView:self.view];
    NSDictionary *param = @{@"eId":[ICUser currentUser].eId,
                            @"iComPassword":iComPwd,
                            @"password":setPayPwd};
    [[ICRedPacketNetworkManager sharedInstance] setRedPacketPasswordWithParam:param success:^(id object) {
        //[MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
        if ([[object objectForKey:@"errno"] intValue] == 0) {
            [weakSelf clearPasswordErrorCount];
            [MBProgressHUD showSuccess:@"密码修改成功"];
            [weakSelf performSelector:@selector(back) withObject:nil afterDelay:1.0];
        }else if ([[object objectForKey:@"errno"] intValue] == 1100) {
            [MBProgressHUD showError:@"用户名或密码错误"];
        }else {
            [MBProgressHUD showError:object[@"error"]];
        }
    } failure:^(NSError *error) {
        //[MBProgressHUD hideAllHUDsForView:weakSelf.view animated:YES];
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
        [MBProgressHUD showError:error.domain];
    }];
}

- (void)back {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)clearPasswordErrorCount {
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"passwordErrorCount"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

#pragma mark - Getter

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
        _scrollView.showsVerticalScrollIndicator = NO;
        [self.view addSubview:_scrollView];
    }
    return _scrollView;
}

- (ICActPwdView *)actPwdView {
    if (!_actPwdView) {
        _actPwdView = [[ICActPwdView alloc] initWithFrame:CGRectMake(0, 0, App_Frame_Width, 500)];
        [self.scrollView addSubview:_actPwdView];
    }
    return _actPwdView;
}

#pragma mark - Getter

- (UILabel *)icomLabel {
    if (!_icomLabel) {
        _icomLabel = [[UILabel alloc] init];
        _icomLabel.text = @"iCom密码";
        _icomLabel.textColor = ICRGB(0x333333);
        _icomLabel.font = ICFont(16.0 * Level);
        [self.scrollView addSubview:_icomLabel];
    }
    return _icomLabel;
}

- (UILabel *)setPayLabel {
    if (!_setPayLabel) {
        _setPayLabel = [[UILabel alloc] init];
        _setPayLabel.text = @"设置支付密码";
        _setPayLabel.textColor = ICRGB(0x333333);
        _setPayLabel.font = ICFont(16.0 * Level);
        [self.scrollView addSubview:_setPayLabel];
    }
    return _setPayLabel;
}

- (UILabel *)confirmPayLabel {
    if (!_confirmPayLabel) {
        _confirmPayLabel = [[UILabel alloc] init];
        _confirmPayLabel.text = @"确认支付密码";
        _confirmPayLabel.textColor = ICRGB(0x333333);
        _confirmPayLabel.font = ICFont(16.0 * Level);
        [self.scrollView addSubview:_confirmPayLabel];
    }
    return _confirmPayLabel;
}

- (UILabel *)promptLabel {
    if (!_promptLabel) {
        _promptLabel = [[UILabel alloc] init];
        _promptLabel.textColor = ICRGB(0xe44e4e);
        _promptLabel.font = ICFont(14.0 * Level);
        [self.scrollView addSubview:_promptLabel];
    }
    return _promptLabel;
}

-(UIButton *)finishButton {
    if (!_finishButton) {
        _finishButton = [[UIButton alloc] init];
        _finishButton.layer.cornerRadius = 5;
        [_finishButton setTitle:@"确定" forState:UIControlStateNormal];
        [_finishButton setTitleColor:[UIColor colorWithWhite:1.0 alpha:0.4] forState:UIControlStateNormal];
        [_finishButton setBackgroundImage:[UIImage imageNamedFromiComeSDKBundle:@"icon_my_redpacket_done"] forState:UIControlStateNormal];
        [_finishButton addTarget:self action:@selector(finishButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:_finishButton];
        _finishButton.enabled = NO;
    }
    return _finishButton;
}

@end
